from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json
from typing import List

mcp = FastMCP("Libgpg-error MCP Server")

"""
面向 libgpg-error 软件的 MCP Server

所有 MCP tool 列表：
1. libgpg_error_get_version - 获取 libgpg-error 库版本信息
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

2. libgpg_error_list_codes - 列出所有 GPG 错误代码
   参数：无  
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

3. libgpg_error_get_defines - 获取所有错误代码的 #define 定义
   参数：无
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}

4. libgpg_error_get_description - 根据错误代码获取错误描述
   参数：error_code (str) - 错误代码，如 "GPG_ERR_SYSTEM_ERROR"
   返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
"""

@mcp.tool()
def libgpg_error_get_version() -> dict:
    """获取 libgpg-error 库版本信息
    
    返回：
        JSON 结构：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
    """
    command = ["gpg-error", "--lib-version"]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def libgpg_error_list_codes() -> dict:
    """列出所有 GPG 错误代码
    
    返回：
        JSON 结构：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
    """
    command = ["gpg-error", "--list"]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def libgpg_error_get_defines() -> dict:
    """获取所有错误代码的 #define 定义
    
    返回：
        JSON 结构：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
    """
    command = ["gpg-error", "--defines"]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def libgpg_error_get_description(error_code: str) -> dict:
    """根据错误代码获取错误描述
    
    参数：
        error_code (str): 错误代码，如 "GPG_ERR_SYSTEM_ERROR"
    
    返回：
        JSON 结构：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
    """
    command = ["gpg-error", "--desc", error_code]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

if __name__ == "__main__":
    mcp.run()